jconed.editor = function ( el, params )
{
	if ( ! jconed._loaded )
	{
		jconed.init ();
		jconed._loaded = true;
	}

	if ( params [ 'fullscreen' ] && ! jconed.toolbar.fullscreen )
		jconed.toolbar.toggle_fullscreen ();

	var self = this;

	this.editing = false;
	this.enabled = true;
	this.mode = null;
	this.cback = {
		'confirm': null,
		'cancel': null
	};
	this.params = params;

	el = $el ( el );
	if ( ! el.id )
		el.id = 'jconed-instance-' + ( jconed._instances.count () + 1);
	this.id = el.id;

	jconed._instances [ this.id ] = this;

	liwe.events.add ( el, 'mouseup', function ( ev ) { jconed.editor.prototype.do_context.call ( self, ev ); } );
	liwe.events.add ( el, 'keyup', function ( ev ) { jconed.editor.prototype.do_context.call ( self, ev ); } );

	this.hover = new jconed.hover ( this, params );

	for ( var i = 0; i < jconed._loaders.length; i++ )
		jconed._loaders [ i ] ( this );
};

jconed.editor.LIGHTBOX = 3;
jconed.editor.FULLSCREEN = 2;
jconed.editor.INLINE = 1;
jconed.editor.DEFAULT = 0;

jconed.editor.prototype.enable = function ()
{
	this.enabled = true;
};

jconed.editor.prototype.disable = function ()
{
	this.enabled = false;
};

jconed.editor.prototype.attach = function ()
{
	var self = this;
	var el = $( this.id );
	this.editing = true;
	el.contentEditable = true;
	liwe.dom.add_class ( el, 'jconed-editor' );
	this._html = el.innerHTML;
	//FIXME: _checkInterval gives problems not clearing properly setInterval
	//this._checkInterval = setInterval ( function () { self._check.call ( self ) }, 500 );
	//el.focus ();
	// creates a fake selection
	var range = document.createRange ();
	if ( ! el.firstChild ) el.appendChild ( document.createElement ( 'span' ) );
	range.setStart ( el.firstChild, 0 );
	range.setEnd ( el.firstChild, 0 );
	jconed.editor._selection = [ range ];
};

jconed.editor.prototype.detach = function ()
{
	this.editing = false;
	var el = $( this.id );
	if ( el )
	{
		el.contentEditable = false;
		liwe.dom.del_class ( el, 'jconed-editor jconed-edit-mode' );
	}
	jconed.editor.current = null;
	if ( ! jconed.toolbar.fullscreen )
		jconed.toolbar.hide ();
};

jconed.editor.prototype.do_edit = function ( mode )
{
	/*
	if ( ! jconed.toolbar.hidden && jconed.editor.current )
	{
		if ( jconed.toolbar.fullscreen )
		{
			jconed.toolbar.execute ( 'Confirm' );
		}
		else
		{
			return;
		}
	}
	*/
	function _set_position ()
	{
		if ( ! jconed.toolbar.container ) return
		var tbwidth = liwe.dom.get_size ( jconed.toolbar.container ) [ 0 ]
		var x, y, delta = ( viewport [ 0 ] - left - width > left ) ? false : true;
		y = top;
		
		if ( delta )
		{
			x = 0;
			if ( left > 500 )
				x = left - ( tbwidth + 10 );
		}
		else
		{
			x = left + width + 10;
		}
		jconed.toolbar.position ( x, y );
	}
	
	if ( typeof mode == 'undefined' || mode == jconed.editor.DEFAULT ) mode = this.mode;
	if ( mode == null ) mode = jconed.editor.INLINE;
	this.mode = mode;

	var fullscreen = false;
	var lightbox = false;
	if ( jconed.editor.current ) jconed.toolbar.execute ( 'Confirm' );

	switch ( mode )
	{
		case jconed.editor.LIGHTBOX:
			lightbox = true;
			break;

		case jconed.editor.FULLSCREEN:
			fullscreen = true;
			break;

		case jconed.editor.INLINE:
			// do nothing
			break;
	}


	//var self = this;
	var el = $( this.id );
	var left = liwe.dom.get_offset_left ( el );
	var top = liwe.dom.get_offset_top ( el );
	var width = liwe.dom.get_size ( el ) [ 0 ];
	var tbwidth = parseInt ( liwe.dom.get_style ( jconed.toolbar.container, 'width' ), 10 );
	var viewport = liwe.dom.get_body_size ();	
	console.debug ( "\nJCONED: el = %o, left = %s, top = %s, width = %s, tbwidth = %s\n\n", el, left, top, width, tbwidth );


	this.hover.hide ();
	jconed.editor.current = this;
	if ( lightbox )
	{
		liwe.lightbox.easy ( 'jconed-lightbox', 'Modifica', 'auto', 'auto' );
		$( 'jconed-lightbox', '<div id="jconed-editable" class="jconed jconed-editor"></div>' );
		$( 'jconed-editable', $( this.id ).innerHTML );
		jconed.toolbar.container.style.zIndex = liwe.dom.get_highest_zindex() + 1;
		jconed.toolbar.show ();
	}
	else
	{
		jconed.toolbar.show ();
		//jconed.toolbar.show ();
		this.attach ();
		if ( fullscreen && ! jconed.toolbar.fullscreen )
		{
			jconed.toolbar.toggle_fullscreen ();
			return;
		}
		_set_position ();
		/*
		//this timeout in some cases avoid conflicting with liwe.dom.hide function
		setTimeout ( function ()
		{
			jconed.toolbar.show ( x, y );
			//jconed.toolbar.show ();
			self.attach ();
			if ( fullscreen && ! jconed.toolbar.fullscreen )
			{
				jconed.toolbar.toggle_fullscreen ();
			}
		}, 100 );
		*/
	}
};

jconed.editor.prototype.do_context = function ( ev )
{
	if ( ! jconed.editor.current ) return false;
	jconed.toolbar.context.innerHTML = '';

	var selection = window.getSelection ();

	jconed.editor._selection = [];
	for ( var i = 0; i < selection.rangeCount; i++ )
		jconed.editor._selection.push ( selection.getRangeAt ( i ) );

	var item = jconed.editor._selection [ 0 ].startContainer;
	var editor_el = $( jconed.editor.current.id );

	if ( ! liwe.dom.is_child_of ( editor_el, item ) && item != editor_el ) return;

	var path = '';
	while ( item && item != $( this.id ) )
	{
		path = ( item.tagName || item.nodeName ) + ( path ? ' > ' + path : '' );
		item = item.parentNode;
	}

	//console.debug ( path );

	jconed.editor._selection.iterate ( function ( s, i )
	{
		console.debug ( "SEL %d -> %o", i, s );
	} );

	for ( var i = 0; i < jconed._context_handlers.length; i++ )
	{
		var handler = jconed._context_handlers [ i ];
		handler ( this );
	}
};

jconed.editor.prototype.set_value = function ( value )
{
	$( this.id ).innerHTML = value;
};

jconed.editor.prototype.get_value = function ()
{
	return $( this.id ).innerHTML;
};

jconed.editor.prototype._check = function ()
{
	if ( ! $( this.id ) )
	{
		this.detach ();
		clearInterval ( this._checkInterval );
	}
};

jconed.editor.get_selection = function ()
{
	if ( ! jconed.editor._selection )
		jconed.editor._selection = [];
	return jconed.editor._selection;
};
